package com.cskaoyan.servlet;

import com.cskaoyan.bean.Categoryl1Data;
import com.cskaoyan.bean.PageData;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.common.CommonServlet;
import com.cskaoyan.model.MarketCategory;
import com.cskaoyan.service.CategoryService;
import com.cskaoyan.service.impl.CategoryServiceImpl;
import com.cskaoyan.utils.JacksonUtil;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


@WebServlet("/admin/category/*")
public class AdminCategoryServlet extends CommonServlet {
    CategoryService categoryService = new CategoryServiceImpl();

    public BaseRespVo list(HttpServletRequest request, HttpServletResponse response) {
        List<MarketCategory> categoryList = categoryService.list();
        PageData data = PageData.list(categoryList);
        return BaseRespVo.ok(data);
    }

    public BaseRespVo create(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        String jsonStr = request.getReader().readLine();
        MarketCategory marketCategory = JacksonUtil.read(jsonStr, MarketCategory.class);
        MarketCategory marketCategory1 = categoryService.create(marketCategory);
        return BaseRespVo.ok(marketCategory1);
    }

    public BaseRespVo update(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        String jsonStr = request.getReader().readLine();
        MarketCategory marketCategory = JacksonUtil.read(jsonStr, MarketCategory.class);
        categoryService.update(marketCategory);
        return BaseRespVo.ok(null);
    }

    public BaseRespVo delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        String jsonStr = request.getReader().readLine();
        MarketCategory marketCategory = JacksonUtil.read(jsonStr, MarketCategory.class);
        categoryService.delete(marketCategory);
        return BaseRespVo.ok(null);
    }

    public BaseRespVo l1(HttpServletRequest request, HttpServletResponse response) {
        List<Categoryl1Data> dataList = categoryService.l1();
        PageData data = PageData.list(dataList);
        return BaseRespVo.ok(data);
    }
}
